package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.pro.ProcessStep;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pro.mapper.ProcessStepMapper;
import com.qidianit.mes.pro.service.ProcessStepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: YKYB
 * @Date: 2025/9/18 15:17
 * @Description:
 */
@Service
public class ProcessStepServiceImpl implements ProcessStepService {

    @Autowired
    private ProcessStepMapper processStepMapper;

    @Override
    public Result<PageInfo<ProcessStep>> selectPageByProcessId(Long processId, int pageNum, int size) {
        //开启分页
        PageHelper.startPage(pageNum, size);
        List<ProcessStep> processSteps = processStepMapper.selectPageByProcessId(processId);
        return Result.success(new PageInfo<>(processSteps));
    }

    @Override
    public Result<ProcessStep> selectById(Long id) {
        ProcessStep step = processStepMapper.selectById(id);
        return step == null ? Result.success(step) : Result.error("工序步骤不存在或已删除");
    }

    @Override
    public Result<ProcessStep> addStep(ProcessStep step) {
        processStepMapper.insert(step);
        return Result.success(step);
    }

    @Override
    public Result<?> updateStep(ProcessStep step) {
        int rows = processStepMapper.updateById(step);
        return rows > 0 ? Result.success("更新成功") : Result.error("工序步骤不存在或已删除");
    }

    @Override
    public Result<?> deleteStep(Long id) {
        int rows = processStepMapper.deleteById(id);
        return rows > 0 ? Result.success("删除成功") : Result.error("工序步骤不存在或已删除");
    }
}
